/**
 * 
 *汇付天下有限公司
 * Copyright (c) 2006-2013 ChinaPnR,Inc.All Rights Reserved.
 */
package com.hft.web.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author feitao.huang
 * @version $Id: DownLoadController.java, v 0.1 2013-7-24 上午10:52:01 feitao.huang Exp $
 */
@Controller
@RequestMapping("/download")
public class DownLoadController {
    private Logger logger = LoggerFactory.getLogger(DownLoadController.class);

    @RequestMapping("/downloadfile")
    public void downloadFile(ModelMap model, HttpServletRequest request,
                             HttpServletResponse response, String name) {
        System.out.println(name);
        OutputStream output = null;
        InputStream input = null;
        try {
            String filePath = "D:/temp/" + name + ".txt";//文件实际路径
            String fileName2Client = "下载.csv";
            logger.debug("使用slf4j");
            response.reset();
            response.setContentType("application/x-download;charset=UTF-8");
            //response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("汉字名称文件", "UTF-8"));
           String filename =  new String(fileName2Client.getBytes("GBK"),"iso-8859-1");//客户端保存每称
            response.addHeader("Content-Disposition","attachment;filename=" +filename);
            File file = new File(filePath);
            if (!file.exists()) {
                return;
            }
            input = new FileInputStream(file);
            output = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[4096];
            int count = 0;
            while ((count = input.read(buffer)) > 0) {
                output.write(buffer, 0, count);
            }

        } catch (IOException e) {
            System.out.println("系统异常：" + e);
        } finally {
            try {
                output.flush();
                output.close();
            } catch (IOException ex) {
                System.out.println("系统异常：" + ex);
            }
        }
    }

    @RequestMapping("/downloadIndex")
    public String downloadIndex(ModelMap model) {
        return "/download/download";
    }

}
